<template>
  <el-dialog
    :modelValue="showDlg"
    :title="title"
    width="500px"
    :before-close="handleClose"
  >
    <div>
      <el-form
        :model="formData"
        label-width="100px"
        ref="formRef"
        :rules="formRule"
        inline
      >
        <el-form-item label="用户名称" prop="username">
          <el-input
            clearable
            v-model="formData.username"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="name">
          <el-input
            clearable
            v-model="formData.name"
            placeholder="请输入用户昵称"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input
            clearable
            :disabled="formData.id"
            v-model="formData.password"
            placeholder="请输入用户密码"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="saveForm(formRef)">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ElButton } from 'element-plus'
import { ref, reactive, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { reqAddOrUpdateUser, reqDetailUser } from '@/api/acl/user/user'
import type { UserData } from '@/api/acl/user/type'

let $emit = defineEmits('closeDlg', 'saveDlg')
import type { FormInstance, FormRules } from 'element-plus'
let props = defineProps({
  showDlg: {
    type: Boolean,
    default: false,
  },
  id: Number,
})
let formData = reactive<UserData>({
  name: '',
  username: '',
  password: '',
  id:0
})
const formRef = ref<FormInstance>()
const formRule = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户名称(至少5位)', trigger: 'blur' ,min:5}],
  name: [{ required: true, message: '请输入用户昵称(至少5位)', trigger: 'blur' ,min:5}],
  password: [{ required: true, message: '请输入用户密码(至少6位)', trigger: 'blur' ,min:6}],
})
onMounted(() => {
  if (props.id) {
    reqDetailUser(props.id).then((res) => {
      console.log('res: ', res);
      if (res.code == 200) {
        Object.assign(formData, res.data)
        console.log('formData: ', formData);
      }
    })
  }
})
const title = computed(() => {
  return props.id ? '修改' : '新增'
})

const handleClose = () => {
  $emit('closeDlg', false)
}
const saveForm = async (formRef: FormInstance | undefined) => {
  if (!formRef) return
  await formRef.validate(async (valid, fields) => {
    if (valid) {
      const res = await reqAddOrUpdateUser(formData)
      console.log(res)
      if (res.code == 200) {
        ElMessage.success(formData.id ? '修改成功' : '新增成功')
      } else {
        ElMessage.warning('新增失败')
      }
      $emit('saveDlg', false)
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>
<style lang="scss" scoped>
::v-deep .el-input,
.el-form-item {
  width: 100%;
}
</style>
